// pages/orderdetails/orderdetails.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    orderid:'',
    order_ID:'',
    hospitalId:'',
    currentOrder:'',
    
  },



//去支付
  toPay(){
    var that = this;
    console.log("openID:"+wx.getStorageSync("openID"))
    wx.request({
      url: this.data.myHttp + '/pay/unifiedOrder',
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      data: {
        id: this.data.order_ID,
        payType: 'wxxcx',
        openId:wx.getStorageSync("openId")
      },
      success(res) {
        console.log(res.data)
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          'success': function (res) {
            console.log("支付成功"+JSON.stringify(res));
            wx.redirectTo({
              url: '../myOrderDetails/myOrderDetails?order_ID=' + that.data.order_ID + '&hospitalId=' + that.data.hospitalId,
            })
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              duration: 2000
            })
          },
          'fail': function (res) {
            console.log(JSON.stringify(res));
            console.log('fail' + JSON.stringify(res))
            wx.showModal({
              title: '提示',
              content: '支付失败，请重新支付',
              showCancel: false
            })
          }
        })
      },
      fail: function (err) {
        console.log("后端支付接口失败"+JSON.stringify(err))
      }
    })

  },



  //取消订单
  toCancel(){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认取消订单？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.toCancel2()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  toCancel2() {
    var that = this
    var token = "Bearer "+wx.getStorageSync('token')
    wx.request({
      url: this.data.myHttp + '/order/orderCancel',
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      data: {
        orderid: that.data.orderid,
      },
      success(res) {
        console.log(res.data)
        wx.switchTab({
          url: '../register/register'
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var token = "Bearer "+wx.getStorageSync('token')
    console.log("医生详情传参"+JSON.stringify(options))
    this.setData({
      order_ID: options.order_ID,
      hospitalId: options.hospitalId
    })
    wx.request({
      url: this.data.myHttp + '/order/getOrderById/' + this.data.order_ID + '/' + this.data.hospitalId,
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Authorization': token
      },
      success(res) {
        console.log(res.data.obj)
        
        that.setData({
          currentOrder:res.data.obj,
          orderid:res.data.obj.orderid
        })
      
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})